/*
 * Copyright (c) 2023, LXMS (lxinet.cn).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.lxinet.lxms.system.service;

import cn.lxinet.lxms.framework.mybatis.service.SimpleService;
import cn.lxinet.lxms.framework.security.entity.LxmsUser;
import cn.lxinet.lxms.framework.security.helper.SecurityHelper;
import cn.lxinet.lxms.system.convert.SysMenuConvert;
import cn.lxinet.lxms.system.dto.sysMenu.SysMenuCreateDto;
import cn.lxinet.lxms.system.dto.sysMenu.SysMenuUpdateDto;
import cn.lxinet.lxms.system.entity.SysMenu;
import cn.lxinet.lxms.system.mapper.SysMenuMapper;
import cn.lxinet.lxms.system.vo.SysMenuVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 菜单
 *
 * @author zcx
 * @date 2023/07/20
 */
@Slf4j
@Service
public class SysMenuService extends SimpleService<SysMenu> {
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 查询菜单列表
     *
     * @return {@link List}<{@link SysMenuVo}>
     */
    public List<SysMenuVo> list() {
        List<SysMenuVo> allMenuList = sysMenuMapper.list();
        return genMenuList(allMenuList);
    }

    /**
     * 获取简单信息的列表
     *
     * @return {@link List}<{@link SysMenuVo}>
     */
    public List<SysMenuVo> simpleList() {
        List<SysMenuVo> allMenuList = sysMenuMapper.simpleList();
        return genMenuList(allMenuList);
    }

    /**
     * 菜单上下级处理
     *
     * @param allMenuList 所有菜单列表
     * @return {@link List}<{@link SysMenuVo}>
     */
    private List<SysMenuVo> genMenuList(List<SysMenuVo> allMenuList){
        List<SysMenuVo> menuList = allMenuList.stream().filter(menu -> menu.getType() == 0).toList();
        menuList.forEach(menu -> menu.setChildren(allMenuList.stream().filter(m -> m.getParentId().equals(menu.getId())).toList()));
        menuList.forEach(menu -> menu.getChildren().forEach(subMenu -> subMenu.setChildren(allMenuList.stream().filter(m -> m.getParentId().equals(subMenu.getId())).toList())));
        return menuList;
    }

    /**
     * 查询用户有权限按钮列表
     *
     * @param roleIds 角色id
     * @return {@link List}<{@link SysMenuVo}>
     */
    public List<SysMenuVo> listButtonByRoleIds(List<Long> roleIds) {
        return sysMenuMapper.listButtonByRoleIds(roleIds);
    }

    /**
     * 查询菜单列表
     *
     * @return {@link List}<{@link SysMenuVo}>
     */
    public List<SysMenuVo> listAllMenu() {
        return sysMenuMapper.listAllMenu();
    }

    /**
     * 查询所有权限列表
     *
     * @return {@link List}<{@link String}>
     */
    public List<String> listAllPermissions() {
        return sysMenuMapper.listAllPermissions();
    }
    /**
     * 查询用户权限列表
     *
     * @param roleIds 角色id
     * @return {@link List}<{@link String}>
     */
    public List<String> listPermissionsByRoleIds(List<Long> roleIds) {
        return sysMenuMapper.listPermissionsByRoleIds(roleIds);
    }

    /**
    * 添加
    * @param dto dto
    */
    public void add(SysMenuCreateDto dto){
        SysMenu sysMenu = SysMenuConvert.INSTANCE.toEntity(dto);
        //新增数据逻辑处理...

        super.insert(sysMenu);
    }

    /**
     * 修改
     * @param dto dto
     * @return {@link Integer}
     */
    public void update(SysMenuUpdateDto dto){
        SysMenu sysMenu = SysMenuConvert.INSTANCE.toEntity(dto);
        //修改数据逻辑处理...

        super.updateById(sysMenu);
    }

    /**
     * 删除
     * @param id id
     */
    public void delete(Long id){
        //删除数据逻辑处理...

        super.deleteById(id);
    }

    public List<Long> listAllIds(){
        return sysMenuMapper.listAllIds();
    }

    public List<SysMenuVo> getUserMenuList(){
        List<SysMenuVo> allMenuList = listAllMenu();
        LxmsUser lxmsUser = SecurityHelper.getLoginUser();
        if (lxmsUser.isAdministrator()){
            List<SysMenuVo> menuList = allMenuList.stream().filter(menu -> menu.getType() == 0).toList();
            List<SysMenuVo> finalAllMenuList = allMenuList;
            menuList.forEach(menu -> menu.setChildren(finalAllMenuList.stream().filter(m -> m.getParentId().equals(menu.getId())).toList()));
            return menuList;
        }else {
            List<SysMenuVo> menuList = allMenuList.stream().filter(menu -> menu.getType() == 0).toList();
            List<Long> roleIds = sysUserRoleService.queryRoleIds(lxmsUser.getId());
            List<SysMenuVo> roleAllBottonList = listButtonByRoleIds(roleIds);
            List<Long> parentMenuIds = roleAllBottonList.stream().map(botton -> botton.getParentId()).toList();
            if (!parentMenuIds.isEmpty()){
                List<SysMenuVo> parentMenuList = sysMenuMapper.listMenuByIds(parentMenuIds);
                menuList.forEach(menu -> menu.setChildren(parentMenuList.stream().filter(m -> m.getParentId().equals(menu.getId())).toList()));
            }
            menuList = menuList.stream().filter(menu -> menu.getChildren() != null && menu.getChildren().size() > 0).toList();
            return menuList;
        }

    }
}